package com.cskaoyan.controller;

import com.cskaoyan.bean.CskaoyanmallAd;
import com.cskaoyan.bean.CskaoyanmallAdExample;
import com.cskaoyan.bean.bo.ExtensionAdBo;
import com.cskaoyan.bean.vo.AdVo;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.mapper.CskaoyanmallAdMapper;
import com.cskaoyan.service.ExtensionAdvertisementService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 推广管理
 * 广告管理
 * by 桂圆
 */
@RestController
public class ExtensionAdvertisementController {

    @Autowired
    ExtensionAdvertisementService extensionAdvertisementService;

    /**
     * 在推广管理/广告管理模块显示所有广告
     *
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     */
//    	GET /admin/ad/list?page=1&limit=20&name=%E4%BD%9B&content=%E6%9D%A5&sort=add_time&order=desc HTTP/1.1
    @RequiresPermissions("admin:ad:list")
    @GetMapping("admin/ad/list")
    public BaseRespVo searchAds(Integer page, Integer limit, String sort, String order, String name, String content){

        AdVo adVo = extensionAdvertisementService.vagueQueryAds(page, limit, sort, order, name, content);

        return BaseRespVo.success(adVo);
    }

    // POST /admin/ad/delete HTTP/1.1

    /**
     * 删除广告--逻辑删除
     */
    @RequiresPermissions("admin:ad:delete")
    @PostMapping("admin/ad/delete")
    public BaseRespVo delAd(@RequestBody CskaoyanmallAd AdBo){

        Integer effectRows = extensionAdvertisementService.deleteAdvertisement(AdBo);

        if (effectRows != 1) {
            // TODO: 返回失败方法
//            return
        }

        return BaseRespVo.success();
    }

    /**
     * 添加广告
     */
//    POST /admin/ad/create HTTP/1.1
    @RequiresPermissions("admin:ad:create")
    @PostMapping("admin/ad/create")
    public BaseRespVo addAd(@RequestBody ExtensionAdBo adBo){

        CskaoyanmallAd adVo= extensionAdvertisementService.addAdvertisement(adBo);

        return BaseRespVo.success(adVo);
    }

    /**
     * 更新广告
     */
//    POST /admin/ad/update HTTP/1.1
    @RequiresPermissions("admin:ad:update")
    @PostMapping("admin/ad/update")
    public BaseRespVo updateAd(@RequestBody ExtensionAdBo adBo){

        extensionAdvertisementService.updateAd(adBo);

        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String updateTime = sf.format(new Date());
        adBo.setUpdateTime(updateTime);

        return BaseRespVo.success(adBo);
    }
}
